package jifenbao.com.widget.ActionBar;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import jifenbao.com.jifenbao.R;

/**
 * Created by Administrator on 2016/6/1 0001.
 */
//类名和attrs命名一样
public abstract class BaseHeaderView extends RelativeLayout{
    protected Context context;
    private LinearLayout actionbarLeft,actionbarCenter,actionbarRight;
    private boolean isShowLeft,isShowCenter,isShowRight;

    public BaseHeaderView(Context context) {
        this(context,null);
    }

    public BaseHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BaseHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        getAttrs(attrs);
        Searchview();
        startView();
    }
    protected  void getAttrs(AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BaseHeaderView);
        isShowLeft = ta.getBoolean(R.styleable.BaseHeaderView_ShowLeft,true);
        isShowCenter = ta.getBoolean(R.styleable.BaseHeaderView_ShowCenter,true);
        isShowRight = ta.getBoolean(R.styleable.BaseHeaderView_ShowRight,true);
        //用完表后回收掉
        ta.recycle();
    }

    public abstract View getLeftView();
    public abstract View getCenterView();
    public abstract View getRightView();

    public void Searchview(){
        View headerview = View.inflate(context,R.layout.widget_base_action,this);
        actionbarLeft = (LinearLayout)headerview.findViewById(R.id.actionbar_left);
        actionbarCenter = (LinearLayout)headerview.findViewById(R.id.actionbar_center);
        actionbarRight = (LinearLayout)headerview.findViewById(R.id.actionbar_right);
    }

    public void startView(){
        actionbarLeft.setVisibility(isShowLeft ? VISIBLE : INVISIBLE);
        actionbarCenter.setVisibility(isShowCenter ? VISIBLE : INVISIBLE);
        actionbarRight.setVisibility(isShowRight ? VISIBLE : INVISIBLE);

        if(getLeftView() != null) actionbarLeft.addView(getLeftView());
        if(getCenterView() != null) actionbarCenter.addView(getCenterView());
        if(getRightView() != null) actionbarRight.addView(getRightView());

        actionbarLeft.setOnClickListener(ClickListener);
        actionbarCenter.setOnClickListener(ClickListener);
        actionbarRight.setOnClickListener(ClickListener);
    }


    private OnClickListener ClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.actionbar_left:
                    if(onHeaderClickListener == null){
                        if(context instanceof Activity){
                            ((Activity) context).finish();
                        }
                    }else onHeaderClickListener.onLeftClicked();
                    break;
                case R.id.actionbar_center:
                    if(onHeaderClickListener !=null) onHeaderClickListener.onCenterClicked();
                    break;
                case R.id.actionbar_right:
                    if(onHeaderClickListener !=null) onHeaderClickListener.onRightClicked();
                    break;
            }
        }
    };


    private onHeaderClickListener onHeaderClickListener;
    public void setOnHeaderClickListener(onHeaderClickListener onHeaderClickListener){
        this.onHeaderClickListener = onHeaderClickListener;
    }
    public interface onHeaderClickListener{
        void onLeftClicked();
        void onCenterClicked();
        void onRightClicked();
    }
}
